package master;

/**
 * 路径总和
 *
 * @author chenyw
 * @date 2022/7/30 15:55
 */
public class M112hasPathSum {
    boolean isTrue = false;
    public boolean hasPathSum(TreeNode root, int target) {
        if (root == null) {
            return false;
        }
        dfs(root, target, 0);
        return isTrue;
    }

    private void dfs(TreeNode root, int target, int sum) {
        if (root == null) {
            return;
        }
        sum += root.val;
        if (root.left == null && root.right == null) {
            if (sum == target) {
                isTrue = true;
                return;
            }
        }
        dfs(root.left, target, sum);
        dfs(root.right, target, sum);
        sum -= root.val;
    }
}
